<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的文章 - 学生知识管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
        }

        .navbar {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            padding: 1rem 0;
            box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .nav-container {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 2rem;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            color: #667eea;
            text-decoration: none;
        }

        .nav-links {
            display: flex;
            list-style: none;
            gap: 2rem;
        }

        .nav-links a {
            text-decoration: none;
            color: #333;
            font-weight: 500;
            transition: color 0.3s ease;
        }

        .nav-links a:hover {
            color: #667eea;
        }

        .container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
            display: flex;
            gap: 2rem;
        }

        .sidebar {
            width: 250px;
            background: white;
            border-radius: 15px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            height: fit-content;
        }

        .profile-avatar {
            text-align: center;
            margin-bottom: 2rem;
        }

        .avatar {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2rem;
            font-weight: bold;
            margin: 0 auto 1rem;
        }

        .sidebar-menu {
            list-style: none;
        }

        .sidebar-menu li {
            margin-bottom: 0.5rem;
        }

        .sidebar-menu a {
            display: block;
            padding: 0.75rem 1rem;
            text-decoration: none;
            color: #666;
            border-radius: 8px;
            transition: all 0.3s ease;
        }

        .sidebar-menu a:hover,
        .sidebar-menu a.active {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
        }

        .main-content {
            flex: 1;
            background: white;
            border-radius: 15px;
            padding: 2rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }

        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
        }

        .page-title {
            font-size: 2rem;
            color: #333;
        }

        .btn {
            background: linear-gradient(135deg, #667eea, #764ba2);
            color: white;
            border: none;
            padding: 0.75rem 2rem;
            border-radius: 25px;
            cursor: pointer;
            font-size: 1rem;
            font-weight: 500;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .articles-grid {
            display: grid;
            gap: 1.5rem;
        }

        .article-card {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 1.5rem;
            transition: all 0.3s ease;
            border: 1px solid #e9ecef;
        }

        .article-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }

        .article-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 1rem;
        }

        .article-title {
            font-size: 1.3rem;
            font-weight: bold;
            color: #333;
            margin-bottom: 0.5rem;
            text-decoration: none;
        }

        .article-title:hover {
            color: #667eea;
        }

        .article-status {
            padding: 0.25rem 0.75rem;
            border-radius: 15px;
            font-size: 0.8rem;
            font-weight: 500;
        }

        .status-published {
            background: #d4edda;
            color: #155724;
        }

        .status-draft {
            background: #fff3cd;
            color: #856404;
        }

        .status-pending {
            background: #cce5ff;
            color: #004085;
        }

        .article-meta {
            display: flex;
            gap: 1rem;
            margin-bottom: 1rem;
            font-size: 0.9rem;
            color: #666;
        }

        .meta-item {
            display: flex;
            align-items: center;
            gap: 0.25rem;
        }

        .article-summary {
            color: #666;
            line-height: 1.6;
            margin-bottom: 1rem;
        }

        .article-actions {
            display: flex;
            gap: 0.5rem;
        }

        .btn-small {
            padding: 0.5rem 1rem;
            font-size: 0.9rem;
            border-radius: 20px;
        }

        .btn-secondary {
            background: #6c757d;
        }

        .btn-secondary:hover {
            box-shadow: 0 5px 15px rgba(108, 117, 125, 0.4);
        }

        .btn-danger {
            background: #dc3545;
        }

        .btn-danger:hover {
            box-shadow: 0 5px 15px rgba(220, 53, 69, 0.4);
        }

        .empty-state {
            text-align: center;
            padding: 4rem 2rem;
            color: #666;
        }

        .empty-icon {
            font-size: 4rem;
            margin-bottom: 1rem;
        }

        .alert {
            padding: 1rem;
            border-radius: 8px;
            margin-bottom: 1rem;
        }

        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .stats-bar {
            display: flex;
            gap: 2rem;
            margin-bottom: 2rem;
            padding: 1rem;
            background: #f8f9fa;
            border-radius: 10px;
        }

        .stat-item {
            text-align: center;
        }

        .stat-number {
            font-size: 1.5rem;
            font-weight: bold;
            color: #667eea;
        }

        .stat-label {
            font-size: 0.9rem;
            color: #666;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar">
        <div class="nav-container">
            <a href="/" class="logo">📚 学生知识管理系统</a>
            <ul class="nav-links">
                <li><a href="/">🏠 首页</a></li>
                <li><a href="/articles">📝 文章</a></li>
                <li><a href="/categories">📂 分类</a></li>
                <li><a href="/profile">👤 个人中心</a></li>
                <li><a href="/logout">🚪 退出</a></li>
            </ul>
        </div>
    </nav>

    <div class="container">
        <!-- 侧边栏 -->
        <aside class="sidebar">
            <div class="profile-avatar">
                <div class="avatar" th:if="${user.avatar == null}" th:text="${user.nickname != null ? user.nickname.substring(0,1) : user.username.substring(0,1)}">U</div>
                <img th:if="${user.avatar != null}" th:src="${user.avatar}" alt="用户头像" class="avatar" style="width: 60px; height: 60px; border-radius: 50%; object-fit: cover;">
                <h3 th:text="${user.nickname != null ? user.nickname : user.username}">用户名</h3>
            </div>
            <ul class="sidebar-menu">
                <li><a href="/profile">📝 个人资料</a></li>
                <li><a href="/profile/avatar">🖼️ 头像管理</a></li>
                <li><a href="/profile/points">⭐ 积分管理</a></li>
                <li><a href="/profile/articles" class="active">📚 我的文章</a></li>
                <li><a href="/profile/favorites">❤️ 我的收藏</a></li>
                <li><a href="/profile/follows">👥 我的关注</a></li>
                <li><a href="/profile/messages">💬 消息中心</a></li>
            </ul>
        </aside>

        <!-- 主要内容 -->
        <main class="main-content">
            <div class="page-header">
                <h1 class="page-title">📚 我的文章</h1>
                <a href="/articles/create" class="btn">✍️ 写文章</a>
            </div>

            <!-- 错误信息 -->
            <div th:if="${error}" class="alert alert-error">
                <span th:text="${error}"></span>
            </div>

            <!-- 统计信息 -->
            <div class="stats-bar">
                <div class="stat-item">
                    <div class="stat-number" th:text="${articles != null ? articles.size() : 0}">0</div>
                    <div class="stat-label">总文章数</div>
                </div>
                <div class="stat-item">
                    <div class="stat-number" th:text="${articles != null ? articles.stream().filter(a -> a.status.name() == 'PUBLISHED').count() : 0}">0</div>
                    <div class="stat-label">已发布</div>
                </div>
                <div class="stat-item">
                    <div class="stat-number" th:text="${articles != null ? articles.stream().filter(a -> a.status.name() == 'DRAFT').count() : 0}">0</div>
                    <div class="stat-label">草稿</div>
                </div>
            </div>

            <!-- 文章列表 -->
            <div th:if="${articles != null and !articles.isEmpty()}">
                <div class="articles-grid">
                    <div class="article-card" th:each="article : ${articles}">
                        <div class="article-header">
                            <div>
                                <a th:href="@{'/articles/' + ${article.id}}" class="article-title" th:text="${article.title}">文章标题</a>
                            </div>
                            <span class="article-status" 
                                  th:class="${'status-' + #strings.toLowerCase(article.status.name())}"
                                  th:text="${article.status.getDisplayName()}">状态</span>
                        </div>
                        
                        <div class="article-meta">
                            <div class="meta-item">
                                <span>📅</span>
                                <span th:text="${#temporals.format(article.createdAt, 'yyyy-MM-dd')}">2024-01-01</span>
                            </div>
                            <div class="meta-item">
                                <span>👁️</span>
                                <span th:text="${article.viewCount ?: 0}">0</span>
                            </div>
                            <div class="meta-item">
                                <span>👍</span>
                                <span th:text="${article.likeCount ?: 0}">0</span>
                            </div>
                            <div class="meta-item">
                                <span>💬</span>
                                <span th:text="${article.commentCount ?: 0}">0</span>
                            </div>
                            <div class="meta-item" th:if="${article.category != null}">
                                <span>📂</span>
                                <span th:text="${article.category.name}">分类</span>
                            </div>
                        </div>
                        
                        <div class="article-summary" th:if="${article.summary != null and !article.summary.isEmpty()}">
                            <p th:text="${#strings.abbreviate(article.summary, 150)}">文章摘要...</p>
                        </div>
                        
                        <div class="article-actions">
                            <a th:href="@{/articles/{id}(id=${article.id})}" class="btn btn-small">查看文章</a>
                            <a th:href="@{/articles/{id}/edit(id=${article.id})}" class="btn btn-small btn-secondary">编辑</a>
                            <a th:href="@{/articles/{id}/delete(id=${article.id})}" class="btn btn-small btn-danger" onclick="return confirm('确定要删除这篇文章吗？')">删除</a>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 空状态 -->
            <div th:if="${articles == null or articles.isEmpty()}" class="empty-state">
                <div class="empty-icon">📝</div>
                <h3>还没有文章</h3>
                <p>开始写您的第一篇文章吧！</p>
                <a href="/articles/create" class="btn" style="margin-top: 1rem;">✍️ 写文章</a>
            </div>
        </main>
    </div>

    <script>
        function deleteArticle(articleId) {
            if (confirm('确定要删除这篇文章吗？此操作不可恢复。')) {
                fetch(`/api/articles/${articleId}`, {
                    method: 'DELETE'
                })
                .then(response => {
                    if (response.ok) {
                        alert('文章删除成功！');
                        location.reload();
                    } else {
                        alert('文章删除失败，请重试。');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('文章删除失败，请重试。');
                });
            }
        }
    </script>
</body>
</html>